/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.account;

import com.ivanbiz.departement.Departement;
import com.ivanbiz.departement.DepartementDAO;
import com.ivanbiz.perpustakaan.KartuPerpus;
import com.ivanbiz.perpustakaan.KartuPerpusDAO;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.tools.MD5;
import com.ivanbiz.tools.RandomList;
import com.ivanbiz.user.User;
import com.ivanbiz.user.UserDAO;
import java.util.List;

/**
 *
 * @author User
 */
public class AccountServiceImpl implements AccountService {

    private AccountDAO accountDAO;
    private KartuPerpusDAO kartuPerpusDAO;
    private UserDAO userDAO;
    private DepartementDAO departementDAO;

    public void setAccountDAO(AccountDAO accountDAO) {
        this.accountDAO = accountDAO;
    }

    public void setKartuPerpusDAO(KartuPerpusDAO kartuPerpusDAO) {
        this.kartuPerpusDAO = kartuPerpusDAO;
    }
    
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }
    
    public void setDepartementDAO(DepartementDAO departementDAO) {
        this.departementDAO = departementDAO;
    }
    
    
    public void saveAccount(Account account) throws Exception {
        boolean accountIsUsed = cekIsEmptyKode(account.getKode());
        if (!accountIsUsed) {
            for (int x = 0; x < account.getAccountCard().size(); x++) {
                AccountCard accountCard = (AccountCard) account.getAccountCard().get(x);
                boolean isEmpty = this.accountDAO.cekIsEmptyCard(accountCard.getNoCard(), accountCard.getNama());
                System.out.println(isEmpty);
                if (!isEmpty) {
                    if (accountCard.getNama().equals(SISConstants.PERPUSTAKAAN)) {
                        KartuPerpus kartuPerpus = new KartuPerpus();
                        kartuPerpus.setKode(accountCard.getNoCard());
                        kartuPerpus.setKodeAccount(account.getKode());
                        this.kartuPerpusDAO.save(kartuPerpus);
                        break;
                    }
                } else {
                    Double.parseDouble(SISConstants.THROWS_ERROR);
                }
            }
            User user = new User();
            user.setUserId(account.getKode());
            user.setPassword(SISConstants.PASSWORD);
            user.setNama(account.getNama());
            RandomList rl = new RandomList();
           // user.setToken(new MD5(rl.getGenerateKey()).toString());
            Departement departement = this.departementDAO.getByNamaBagian(SISConstants.GURU);
            user.setDepartement(departement);
            this.userDAO.save(user);
            this.userDAO.flushUser();
            account.setUser(user);
            account.setToken(new MD5(rl.getGenerateKey()).toString());
            this.accountDAO.save(account);
        }else{
            Double.parseDouble(SISConstants.CODE_IS_USED);
        }
        
    }

    public void deleteAccount(Account account) throws Exception {
        this.accountDAO.delete(account);
    }

    public void updateAccount(Account account) throws Exception {
        for (int x = 0; x < account.getAccountCard().size(); x++) {
            AccountCard accountCard = (AccountCard) account.getAccountCard().get(x);
            // boolean isEmpty = this.accountDAO.cekIsEmptyCard(accountCard.getNoCard(), accountCard.getNama());
            //  if (!isEmpty) {
            if (accountCard.getNama().equals(SISConstants.PERPUSTAKAAN)) {
                KartuPerpus kartuPerpus = (KartuPerpus) this.kartuPerpusDAO.getDataByEqual(KartuPerpus.class, "kodeAccount", accountCard.getAccount().getKode());
                kartuPerpus.setKode(accountCard.getNoCard());
                kartuPerpus.setKodeAccount(account.getKode());
                this.kartuPerpusDAO.update(kartuPerpus);
                break;
            }
        // }else {
        //     Double.parseDouble("throws error");
        // }
        }
        this.accountDAO.update(account);
    }

    public List loadAll() throws Exception {
        return this.accountDAO.getAll(Account.class);
    }

    public Account loadById(long id) throws Exception {
        return (Account) this.accountDAO.load(Account.class, id);
    }

    public boolean cekIsEmptyCard(String kode, String nama) throws Exception {
        return this.accountDAO.cekIsEmptyCard(kode, nama);
    }

    public boolean cekIsEmptyKode(String kode) throws Exception {
        return this.accountDAO.cekIsEmptyKode(kode);
    }

    

    
}
